// 3. 创建带参数的构造函数工厂实例方法
class TestClass {
  // 准备类
  public name: string
  public classno: number
  public test: number
  constructor(name: string, classno: number, test: number) {
    this.name = name
    this.classno = classno
    this.test = test
  }
  eat() {
    console.log('姓名为: ' + this.name, this.classno, this.test)
  }
}
type Constructor<T> = new (...args: any[]) => T // 构造函数类型的通用类型

type ConstructorParametersType1<T extends new (...args: any[]) => any> =
  T extends new (...args: infer P) => any ? P : never

// let constructorParameters: ConstructorParametersType1<typeof TestClass>

// CP 要满足构造函数通用类型 所以 CP extends new (...args: any[]) => any
function createInstance<T, CP extends new (...args: any[]) => any>(
  constructor: Constructor<T>,
  ...args: ConstructorParametersType1<CP>
) {
  //   ...args: ConstructorParametersType1<typeof TestClass>
  //   ...args: [username: string, classno: number]) {
  return new constructor(...args)
}

// 此时还有潜在的问题 多传递 或者少传 传错都不会有错误提示 因为 ...args: any[]  any没有检测能力
/**
 * 将参数改为 ...args: [string, number] 和 ConstructorParametersType1<typeof TestClass> 这样似乎解决问题了，但是不通用
 *
 */
createInstance<TestClass, typeof TestClass>(TestClass, 'wagw', 124124124, 124124).eat()
export {}
